package com.gcit.seniorWish.web.core.shiro.factory;

import com.gcit.seniorWish.core.util.SpringContextHolder;
import com.gcit.seniorWish.web.common.constant.factory.ConstantFactory;
import com.gcit.seniorWish.web.common.constant.enums.ManagerStatusEnum;
import com.gcit.seniorWish.web.common.persistence.model.User;
import com.gcit.seniorWish.web.core.shiro.ShiroUser;
import com.gcit.seniorWish.web.modular.system.dao.MenuDao;
import com.gcit.seniorWish.web.modular.system.dao.UserMgrDao;

import org.apache.shiro.authc.CredentialsException;
import org.apache.shiro.authc.LockedAccountException;
import org.apache.shiro.authc.SimpleAuthenticationInfo;
import org.apache.shiro.crypto.hash.Md5Hash;
import org.apache.shiro.util.ByteSource;
import org.springframework.context.annotation.DependsOn;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service
@DependsOn("springContextHolder")
@Transactional(readOnly = true)
public class ShiroFactroy implements IShiro {

	@Resource
	private UserMgrDao userMgrDao;

	@Resource
	private MenuDao menuDao;

	public static IShiro me() {
		return SpringContextHolder.getBean(IShiro.class);
	}

	@Override
	public User user(String account) {

		User user = userMgrDao.getByAccount(account);
		// 账号不存在
		if (null == user) {
			throw new CredentialsException();
		}
		// 账号被冻结
		if (user.getStatus() != ManagerStatusEnum.ABLE.getCode()) {
			throw new LockedAccountException();
		}
		return user;
	}

	@Override
	public ShiroUser shiroUser(User user) {
		ShiroUser shiroUser = new ShiroUser();

		shiroUser.setId(user.getId()); // 账号id
		shiroUser.setAccount(user.getAccount());// 账号
		shiroUser.setDeptId(user.getDeptId()); // 部门id
		shiroUser.setDeptName(ConstantFactory.me().getDeptName(user.getDeptId()));// 部门名称
		shiroUser.setName(user.getName()); // 用户名称
		shiroUser.setType(user.getType()); // 用户类型
		shiroUser.setSchoolId(user.getSchoolId());
		shiroUser.setClassId(user.getClassId());
		shiroUser.setDataScope(user.getDataScope());// 设置数据范围
		List<String> roleIdList = user.getRoleIdList();
		List<String> roleNameList = new ArrayList<String>();
		for (String roleId : roleIdList) {
			roleNameList.add(ConstantFactory.me().getSingleRoleName(roleId));
		}
		shiroUser.setRoleIdList(roleIdList);
		shiroUser.setRoleNames(roleNameList);
		return shiroUser;
	}

	@Override
	public List<String> findPermissionsByRoleId(String roleId) {
		List<String> resUrls = menuDao.getResUrlsByRoleId(roleId);
		return resUrls;
	}

	@Override
	public String findRoleNameByRoleId(String roleId) {
		return ConstantFactory.me().getSingleRoleRemark(roleId);
	}

	@Override
	public SimpleAuthenticationInfo info(ShiroUser shiroUser, User user, String realmName) {
		String credentials = user.getPassword();
		// 密码加盐处理
		String source = user.getSalt();
		ByteSource credentialsSalt = new Md5Hash(source);
		return new SimpleAuthenticationInfo(shiroUser, credentials, credentialsSalt, realmName);
	}

}
